<?php
/**
 * URL管理类
 * false  ：默认模式，不对URL做任何处理。
 * path ：path模式。
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CUrlManager.php 2009-12-12 23:31:17Z andery $
 */
class CUrlManager {
    private static $_urlconf;//urlmanager配置信息
    private $_controller; //控制器
    private $_action; //方法
    private $_params = null; //URL参数数组
    private $_pathinfo; //应用程序所在目录名

    public function __construct(){
        self::$_urlconf = CConf::get('urlmanager');
        $this->setPathInfo();
        $this->parseUrl();
    }
    
    /**
     * 获得当前URL
     * @return string
     */
    public function getUrl()
    {
        return $this->getHostInfo().$_SERVER['REQUEST_URI'];
    }
    
    /**
     * 获得URL的base部分
     * @return string
     */
    public function baseUrl($url)
    {
        if(strpos($url, $this->getBaseFile())) {
            return $this->getHostInfo().$_SERVER['SCRIPT_NAME'];
        } else {
            return $this->getHostInfo().$this->getPath();
        }
    }
    
    /**
     * 获得当前的文件名
     * @return string
     */
    public function getBaseFile()
    {
        return $this->_pathinfo['basename'];
    }
    
    /**
     * 获得应用程序所在的目录
     * @return string
     */
    public function getPath()
    {
        return $this->_pathinfo['dirname'];
    }
    
    /**
     * 对SCRIPT_NAME进行解析
     * @return array
     */
    public function setPathInfo()
    {
        $this->_pathinfo = pathinfo($_SERVER['SCRIPT_NAME']);
    }
    
    /**
     * URL解析
     * 
     */
    public function parseUrl()
    {
    	$url = $this->getUrl();
        $request=trim(str_replace($this->baseUrl($url), '', $url), '/');
        
        if(self::$_urlconf['urlrewrite']==='path'){
        	//规则部分
        	$rules = self::$_urlconf['rules'];
            if(count($rules)>0) {
                foreach($rules as $key=>$value) {
                    $request = preg_replace($key, $value, $request);
                }
        	}
            $splits = explode('/', $request);
            $this->_controller = !empty($splits[0])?array_shift($splits):Nimo::app()->conf('system.default_controller');
            $this->_action = !empty($splits[0])?array_shift($splits):Nimo::app()->conf('system.default_action');
            $temkey = '';
            foreach($splits as $key=>$value) {
                if($key%2==0){
                	$temkey = $value;
                	$_REQUEST[$temkey] = $_GET[$temkey] = $this->_params[$temkey] = '';
                } else {
                    $_REQUEST[$temkey] = $_GET[$temkey] = $this->_params[$temkey] = $value;
                }
            }
            unset($temkey);
        } elseif (self::$_urlconf['urlrewrite']==='false') {
            $this->_controller = !empty($_GET['m'])?$_GET['m']:CConf::get('system.default_controller');
            $this->_action = !empty($_GET['a'])?$_GET['a']:CConf::get('system.default_action');
        }
    }
    
    /**
     * 生成模板需要的URL地址
     * 
     */
    public function creatUrl()
    {
        
    }
    
    /**
     * 获得域名信息
     * @return string
     */
    public function getHostInfo()
    {
        if(isset($_SERVER['HTTP_HOST']))
            $hostInfo='http://'.$_SERVER['HTTP_HOST'];
        else
        {
            $hostInfo='http://'.$_SERVER['SERVER_NAME'];
            $port=$_SERVER['SERVER_PORT'];
            if( $port!=80 || $port!=443)
                $hostInfo.=':'.$port;
        }
        return $hostInfo;
    }
    
    /**
     * 获取当前控制器名称
     * @return string
     */
    public function getController()
    {
        return $this->_controller;
    }
    
    /**
     * 获取当前控制器需要调用的方法名
     * @return string
     */
    public function getAction()
    {
        return $this->_action;
    }
    
    public function getParams()
    {
        return $this->_params;
    }
}